import router from '@/router'
import { Toast } from 'mint-ui';
import store from '../store/index'
import {
    // localStorage as LsyStorage,
    cookie as LsyCookie
} from 'lsy-storages';

console.log('Cookies', LsyCookie.getAll())

//判断用户是否登陆
const is_login= ()=> {
    if(getToken()){
        return true
    }
    return false
}

//获取touken
const getToken=() => {
    let token = LsyCookie.getItem('token')
    if (token && token != null) {
        return token;
    } else {
        return null
    }
}


// 设置用户信息
const setUid = (uid) => {
    LsyCookie.setItem('uid', uid);
}
const getUid = function () {
    return LsyCookie.getItem('uid');
};

// 设置用户信息
const setToken = (token) => {
    LsyCookie.setItem('token', token);
}

// 设置用户信息
const setUinfo = (user) => {
    LsyCookie.setItem('token', user.token);
    LsyCookie.setItem('uid', user.id);
    LsyCookie.setItem('nickname', user.nickname);
    LsyCookie.setItem('avatar', user.avatar);
    LsyCookie.setItem('mobile', user.mobile);
}
const setNickname =(nickname) =>{
    LsyCookie.setItem('nickname', nickname);
}
const setavatar =(avatar) =>{
    LsyCookie.setItem('avatar', avatar);
}
const getUinfo = function () {
    let userinfo = {
        token : LsyCookie.getItem('token'),
        id: LsyCookie.getItem('uid'),
        nickname: LsyCookie.getItem('nickname'),
        avatar: LsyCookie.getItem('avatar'),
        mobile: LsyCookie.getItem('mobile')
    };
    return userinfo;
};

// 退出
const LogOut = function () {
    LsyCookie.setItem('token', null)
};
const jumpLogin = function() {
    router.push('/m/login')
    Toast('请先登陆');
}
const getQueryUid = function () {
    let auth_id = store.state.auth_id;
    let user_id = LsyCookie.getItem('uid');

    //打开谁的微站
    return auth_id ? auth_id : user_id;
};


const wechatLogin = (domain = 'http://hbmm.test.com', appId = 'wx9456e8a8d912692a') => {
    var dURL = document.URL
    const redirectUrl = encodeURIComponent(domain+'/api/user/third?platform=wechatgzh&redirect_url='+dURL)
    const authUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+appId+'&redirect_uri='+redirectUrl+'&response_type=code&scope=snsapi_userinfo'
    window.location.href = authUrl
}


export {
    is_login,
    getToken,
    jumpLogin,
    setUinfo,
    setToken,
    setUid,
    LogOut,
    getUinfo,
    wechatLogin,
    getQueryUid,
    getUid,
    setNickname,
    setavatar
}

export default {
    is_login,
    getToken,
    jumpLogin,
    setUinfo,
    setToken,
    setUid,
    LogOut,
    getUinfo,
    wechatLogin,
    getQueryUid,
    getUid,
    setNickname,
    setavatar
}